import java.util.*; 
import java.io.*; 
/**
* This is the driver class that contains the main method. It has several 
* static methods that run the program. It has 3 linked lists of type fruit
* one has both oranges and apples, others just one of each. This class works with Fruit, 
* Orange and Apple class.
* 
* <p><b>
* Extra Credit:
* </b><pre>
* No extra credit was offered.
* 
* </pre><b>
* 
* History:
* </b><pre>
* Started 09/28/2007
* Finished 10/04/2007
* </pre>
* 
* @author Veniamin Kozubenko
* @see "java.util.LinkedList, java.util.Collections.sort()"
* @see "No borrowed code"
*/
public class Asg1
{
	public static LinkedList<Fruit> linkedList = new LinkedList<Fruit>(); 
	public static LinkedList<Fruit> linkedListOfApples = new LinkedList<Fruit>();	
	public static LinkedList<Fruit> linkedListOfOranges = new LinkedList<Fruit>();
	public static String inFile = "";
	
	public static void main(String [] args)
	{
		getFileName();
		Scanner fin = openFile(inFile);
		readInputFile(fin);
		printLinkedList("Unsorted list",linkedList);
		
		sortLinkedList();
		printLinkedList("\n\nFully sorted list", linkedList);
		
		splitLinkedList();
		printLinkedList("\n\nList of apples", linkedListOfApples);
		printLinkedList("\n\nList of oranges", linkedListOfOranges);
	} // End main method
	
	public static Scanner openFile(String inFile)
	{
			Scanner fin;
			if (inFile.equals(""))
				return null;
			else
			{
				try 
					{	fin = new Scanner(new File(inFile)); }
							
				catch (IOException e) 
					{
						System.out.println("File did not open! IOException! Please try again. ");
						inFile = getFileName();
						fin = openFile(inFile);
					} // End catch
			}  // End else clause
			return fin;
	} // End method openFile
	
	public static void readInputFile(Scanner fin)
	{
		if(fin == null)
		{
			System.out.println("Blank line was entered! Exiting program...");
			System.exit(-1);
		} // End if statement
		System.out.println("\n\nInput file: " + inFile);
		while(fin.hasNext())
		{
			String fruitType, fruitName;
			double tempPrice;
			
			fruitType = fin.nextLine();
			fruitName = fin.nextLine();
			tempPrice = fin.nextDouble();
			fin.nextLine();
			int price = (int)(tempPrice * 100);
			
			if (fruitType.equalsIgnoreCase("Orange"))		
			{
				Orange orange = new Orange(fruitName, price);
				linkedList.add(orange);
			} // End if statement	
			else if (fruitType.equalsIgnoreCase("Apple"))		
			{
				Apple apple = new Apple(fruitName, price);
				linkedList.add(apple);
			} // End else if
			else
				System.out.println("Improper File Format, the program might crash");
		} // End while loop
		fin.close();
	} // End readInputFile
	
	public static void printLinkedList(String temp, LinkedList<Fruit> list)
	{	
		if(list.size() != 0)
		{	
			System.out.println(temp);
			for (Fruit fruit : list)
				System.out.println(fruit);
		} // End if statement
		else
			System.out.println(temp + "\nThe list is empty! Unable to print list.");
	} // End method printLinkedList

	public static String getFileName()
	{
		Scanner kb = new Scanner(System.in);
		System.out.printf("Please enter file name here --> ");
		inFile = kb.nextLine();
		return inFile;
	} // End method getFileName

	public static void sortLinkedList()
	{ Collections.sort(linkedList); } 
	
	public static void splitLinkedList()
	{
		for (Fruit fruit : linkedList)
			if (fruit instanceof Orange)
				linkedListOfOranges.add(fruit);
			else
				linkedListOfApples.add(fruit);
	} // End method splitLinkedList

} // End class Asg1